package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.TreeShop;
import com.ruoyi.system.domain.equipment.EquipmentType;
import com.ruoyi.system.mapper.equipment.EquipmentTypeMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IEquipmentTypeService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备类型Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-27
 */
@Service
public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, EquipmentType> implements IEquipmentTypeService {

    @Override
    public List<EquipmentType> selectShopList(EquipmentType equipmentType) {
        QueryWrapper<EquipmentType> qw = new QueryWrapper<EquipmentType>();
        List<EquipmentType> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<TreeShop> buildShopTreeSelect(List<EquipmentType> list) {
        List<EquipmentType> shopTrees = buildShopTree(list);
        return shopTrees.stream().map(TreeShop::new).collect(Collectors.toList());
    }

    @Override
    public String checkDeptNameUnique(EquipmentType equipmentType) {
        Long deptId = StringUtils.isNull(equipmentType.getTypeId()) ? -1L : equipmentType.getTypeId();
        EquipmentType info = baseMapper.selectOne(new QueryWrapper<EquipmentType>().eq("type_name", equipmentType.getTypeName())
                .eq("parent_id", equipmentType.getParentId()));
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertShop(EquipmentType equipmentType) {
        //如果父级节点为0或者空直接新增
        if(equipmentType.getParentId()==0||null==equipmentType.getParentId()){
            return baseMapper.insert(equipmentType);
        }else {
            EquipmentType info = baseMapper.selectOne(new QueryWrapper<EquipmentType>().eq("type_id", equipmentType.getParentId()));
            // 如果父节点不为正常状态,则不允许新增子节点
            if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
            {
                throw new CustomException("设备类型停用，不允许新增");
            }
            equipmentType.setAncestors(info.getAncestors() + "," + equipmentType.getParentId());
            return baseMapper.insert(equipmentType);
        }
    }

    /**
     * 构建前端所需要树结构
     *
     * @param
     * @return 树结构列表
     */
    public List<EquipmentType> buildShopTree(List<EquipmentType> shops)
    {
        List<EquipmentType> returnList = new ArrayList<EquipmentType>();
        List<Long> tempList = new ArrayList<Long>();
        for (EquipmentType dept : shops)
        {
            tempList.add(dept.getTypeId());
        }
        for (Iterator<EquipmentType> iterator = shops.iterator(); iterator.hasNext();)
        {
            EquipmentType dept = (EquipmentType) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(shops, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = shops;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<EquipmentType> list, EquipmentType t)
    {
        // 得到子节点列表
        List<EquipmentType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (EquipmentType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<EquipmentType> it = childList.iterator();
                while (it.hasNext())
                {
                    EquipmentType n = (EquipmentType) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<EquipmentType> getChildList(List<EquipmentType> list, EquipmentType t)
    {
        List<EquipmentType> tlist = new ArrayList<EquipmentType>();
        Iterator<EquipmentType> it = list.iterator();
        while (it.hasNext())
        {
            EquipmentType n = (EquipmentType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getTypeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<EquipmentType> list, EquipmentType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
